/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package softwaredevendasjava;

import cliente.Carrinho;
import estoque.EstoqueDeProdutos;
import estoque.Produto;
import estoque.Sugestao;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Gustavo
 */
public class SoftwareDeVendas extends javax.swing.JFrame {

    /**
     * Creates new form SoftwareDeVendas
     */
    private EstoqueDeProdutos estoque;
    private Listar listar;
    private ListarComprados listarComprados;
    private ComprarPanel comprarPanel;
    private Sugestao sugestao;
    private Carrinho carrinho;

    public SoftwareDeVendas() {
        initComponents();

        //Código necessita de refatoração;
        this.estoque = new EstoqueDeProdutos();
        this.carrinho = new Carrinho();
        this.comprarPanel = new ComprarPanel(new Acao() {
            @Override
            public void acao(String... args) {
                changePanelPrincipal(listar);
                carrinho.adicionarNoCarrinho(estoque.getProdutoById(Integer.parseInt(args[1])), Integer.parseInt(args[2]));
                JOptionPane.showMessageDialog( getRootPane(), "Obrigado por comprar em nossa loja virtual :)", "Compra efetuada com sucesso", JOptionPane.PLAIN_MESSAGE);
            }
        }, new Acao() {
            @Override
            public void acao(String... args) {
                changePanelPrincipal(listar);
            }
        });
        this.listar = new Listar(new Acao() {
            @Override
            public void acao(String... args) {
                comprarPanel.setProduto(estoque.getProdutoById(Integer.parseInt(args[0])+1));
                changePanelPrincipal(comprarPanel);
            }
        });
        
        this.listarComprados = new ListarComprados(this.carrinho);
        
        this.sugestao = new Sugestao();
        darSugestao();
        

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelPrincipal = new javax.swing.JPanel();
        panelSugestoes = new javax.swing.JPanel();
        panelSugestao1 = new javax.swing.JPanel();
        txtSNome1 = new javax.swing.JLabel();
        txtSDesc1 = new javax.swing.JLabel();
        txtSPreco1 = new javax.swing.JLabel();
        panelSugestao2 = new javax.swing.JPanel();
        txtSNome2 = new javax.swing.JLabel();
        txtSDesc2 = new javax.swing.JLabel();
        txtSPreco2 = new javax.swing.JLabel();
        panelSugestao4 = new javax.swing.JPanel();
        txtSNome3 = new javax.swing.JLabel();
        txtSDesc3 = new javax.swing.JLabel();
        txtSPreco3 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuIVenda = new javax.swing.JMenuItem();
        menuIComprados = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panelPrincipal.setBorder(javax.swing.BorderFactory.createTitledBorder("Principal"));

        javax.swing.GroupLayout panelPrincipalLayout = new javax.swing.GroupLayout(panelPrincipal);
        panelPrincipal.setLayout(panelPrincipalLayout);
        panelPrincipalLayout.setHorizontalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelPrincipalLayout.setVerticalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 166, Short.MAX_VALUE)
        );

        panelSugestoes.setBorder(javax.swing.BorderFactory.createTitledBorder("Sugestões"));

        txtSNome1.setText("jLabel1");

        txtSDesc1.setText("jLabel2");

        txtSPreco1.setText("jLabel3");

        javax.swing.GroupLayout panelSugestao1Layout = new javax.swing.GroupLayout(panelSugestao1);
        panelSugestao1.setLayout(panelSugestao1Layout);
        panelSugestao1Layout.setHorizontalGroup(
            panelSugestao1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSugestao1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelSugestao1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSDesc1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .addComponent(txtSNome1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtSPreco1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelSugestao1Layout.setVerticalGroup(
            panelSugestao1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSugestao1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtSNome1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSDesc1, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSPreco1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        txtSNome2.setText("jLabel1");

        txtSDesc2.setText("jLabel2");

        txtSPreco2.setText("jLabel3");

        javax.swing.GroupLayout panelSugestao2Layout = new javax.swing.GroupLayout(panelSugestao2);
        panelSugestao2.setLayout(panelSugestao2Layout);
        panelSugestao2Layout.setHorizontalGroup(
            panelSugestao2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSugestao2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelSugestao2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSDesc2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                    .addComponent(txtSNome2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtSPreco2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelSugestao2Layout.setVerticalGroup(
            panelSugestao2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSugestao2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtSNome2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSDesc2, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSPreco2, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        txtSNome3.setText("jLabel1");

        txtSDesc3.setText("jLabel2");

        txtSPreco3.setText("jLabel3");

        javax.swing.GroupLayout panelSugestao4Layout = new javax.swing.GroupLayout(panelSugestao4);
        panelSugestao4.setLayout(panelSugestao4Layout);
        panelSugestao4Layout.setHorizontalGroup(
            panelSugestao4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSugestao4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelSugestao4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSDesc3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                    .addComponent(txtSNome3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtSPreco3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelSugestao4Layout.setVerticalGroup(
            panelSugestao4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSugestao4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtSNome3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSDesc3, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSPreco3, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout panelSugestoesLayout = new javax.swing.GroupLayout(panelSugestoes);
        panelSugestoes.setLayout(panelSugestoesLayout);
        panelSugestoesLayout.setHorizontalGroup(
            panelSugestoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSugestoesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelSugestao1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(panelSugestao2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(panelSugestao4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(43, 43, 43))
        );
        panelSugestoesLayout.setVerticalGroup(
            panelSugestoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelSugestao1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panelSugestao2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panelSugestao4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jMenu1.setText("Simulador");

        menuIVenda.setText("Listar Produtos a Venda");
        menuIVenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuIVendaActionPerformed(evt);
            }
        });
        jMenu1.add(menuIVenda);

        menuIComprados.setText("Listar Produtos Comprados");
        menuIComprados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuICompradosActionPerformed(evt);
            }
        });
        jMenu1.add(menuIComprados);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelSugestoes, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(panelSugestoes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuIVendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuIVendaActionPerformed
        changePanelPrincipal(this.listar);
    }//GEN-LAST:event_menuIVendaActionPerformed

    private void menuICompradosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuICompradosActionPerformed
        this.listarComprados.atualizarPainel();
        changePanelPrincipal(this.listarComprados);
    }//GEN-LAST:event_menuICompradosActionPerformed

    private void changePanelPrincipal(Component c) {
        this.panelPrincipal.removeAll();
        c.setVisible(true);
        c.setPreferredSize(new Dimension(100, 100));
        c.setBounds(new Rectangle(15, 20, this.panelPrincipal.getWidth() - 30, this.panelPrincipal.getHeight() - 30));
        this.panelPrincipal.add(c);
        this.panelPrincipal.revalidate();
        this.panelPrincipal.repaint();
    }
    
    private void darSugestao(){
        Produto[] p = this.sugestao.darSugestao(this.estoque, 3);
        
        this.txtSNome1.setText(p[0].getNome());
        this.txtSDesc1.setText(p[0].getDescricao());
        this.txtSPreco1.setText("R$ "+p[0].getPreco());
        
        this.txtSNome2.setText(p[1].getNome());
        this.txtSDesc2.setText(p[1].getDescricao());
        this.txtSPreco2.setText("R$ "+p[1].getPreco());
        
        this.txtSNome3.setText(p[2].getNome());
        this.txtSDesc3.setText(p[2].getDescricao());
        this.txtSPreco3.setText("R$ "+p[2].getPreco());
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SoftwareDeVendas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SoftwareDeVendas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SoftwareDeVendas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SoftwareDeVendas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame jf = new SoftwareDeVendas();
                jf.setVisible(true);
                jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem menuIComprados;
    private javax.swing.JMenuItem menuIVenda;
    private javax.swing.JPanel panelPrincipal;
    private javax.swing.JPanel panelSugestao1;
    private javax.swing.JPanel panelSugestao2;
    private javax.swing.JPanel panelSugestao4;
    private javax.swing.JPanel panelSugestoes;
    private javax.swing.JLabel txtSDesc1;
    private javax.swing.JLabel txtSDesc2;
    private javax.swing.JLabel txtSDesc3;
    private javax.swing.JLabel txtSNome1;
    private javax.swing.JLabel txtSNome2;
    private javax.swing.JLabel txtSNome3;
    private javax.swing.JLabel txtSPreco1;
    private javax.swing.JLabel txtSPreco2;
    private javax.swing.JLabel txtSPreco3;
    // End of variables declaration//GEN-END:variables
}
